
/**
 *Set Default Value
 */

$(document).ready(function(){
	setDefaultValue();
	// initialise menu plugins
});

function setDefaultValue()
{
	$('.default-value').focus(function(){
		clearText($(this));
	});

	$('.default-value').blur(function(){
		clearText($(this));
	});

	clearText($('.default-value'));
}

function clearText(field){

    if (field.attr('rel') == field.val())
		field.val('');
    else if (field.val() == '')
		field.val(field.attr('rel'));
}

function valert(msg, title , width)
{
	if(width === undefined) width = 400;
	if(title === undefined) title = 'Info';

	$('<div id="i-dialog"></div>').dialog({
		width:400,
		modal:true,
		resizable:false,
		position:'center',
		title:title,
		close:function(){$('#i-dialog').remove();},
		buttons:{'Ok':function(){$(this).dialog('close');}},
		open:function(){
				$(this).html(msg);
		}
	});
}

function vconfirm(msg, callbackFunc, title , width)
{
	if(width === undefined) width = 400;
	if(title === undefined) title = 'Confirm';

	$('<div id="c-dialog"></div>').dialog({
		width:400,
		modal:true,
		resizable:false,
		position:'center',
		title:title,
		close:function(){$('#c-dialog').remove();},
		buttons:{ 'Yes': function(){if (typeof callbackFunc == "function") callbackFunc(this); else alert('Callback function not defiened');},
				  'No':function(){$(this).dialog('close');}
				  },
		open:function(){
				$(this).html(msg);
		}
	});
}

/**
 * @param string msg displaying text
 */
function showAjaxLoading(show)
{
	var content = '<img src="/images/loader.gif"/>';

	if(show == true)
	{
		$('<div id="ajaxLoader"></div>').dialog({
			width:40,
			height: 100,
			modal:true,
			resizable:false,
			position:'center',
			open:function(){$('.ui-dialog').removeClass('ui-widget-content');$(this).html(content);},
			close:function(){$('#ajaxLoader').remove();}
		});
		$('#ajaxLoader').dialog('widget').find(".ui-dialog-titlebar").hide();
	}
	else
	{
		$('#ajaxLoader').dialog('close');
	}

//	$('.ajax-loader').html(msg);
//	if(msg == '')
//		$('.ajax-loader').css('display','none');
//	else
//		$('.ajax-loader').css('display','block');
}

$.extend($.validator, {
	errorElement: ""
});

function loadImages(imgArr,callback) {
	//Keep track of the images that are loaded
	var imagesLoaded = 0;
	function _loadAllImages(callback){
		//Create an temp image and load the url
		var img = new Image();
		$(img).attr('src',imgArr[imagesLoaded]);
		if (img.complete || img.readyState === 4) {
			// image is cached
			imagesLoaded++;
			//Check if all images are loaded
			if(imagesLoaded == imgArr.length) {
				//If all images loaded do the callback
				callback();
			} else {
				//If not all images are loaded call own function again
				_loadAllImages(callback);
			}
		} else {
			$(img).load(function(){
				//Increment the images loaded variable
				imagesLoaded++;
				//Check if all images are loaded
				if(imagesLoaded == imgArr.length) {
					//If all images loaded do the callback
					callback();
				} else {
					//If not all images are loaded call own function again
					_loadAllImages(callback);
				}
			});
		}
	};
	_loadAllImages(callback);
}

